<?php
defined('OA_LIB_PATH') || define('OA_LIB_PATH', '/opt/op/OALib');
defined('OPEN_LIB_PATH') || define('OPEN_LIB_PATH', '/opt/op/OpenLib');

require_once OPEN_LIB_PATH . '/open/models/OaAppModel.php';
require_once OPEN_LIB_PATH . '/open/business-logic/OaLogic.php';
require_once OPEN_LIB_PATH . '/open/business-logic/service/OaServiceSync.php';
require_once OPEN_LIB_PATH . '/open/interfaces/libs/PaymentInterface.php';
require_once OPEN_LIB_PATH . '/open/interfaces/libs/HelpInterface.php';
require_once OPEN_LIB_PATH . '/open/interfaces/libs/DividendInterface.php';
require_once OPEN_LIB_PATH . '/open/interfaces/libs/IntegralInterface.php';


class OaServiceHelper extends OaLogic
{
	/**
	 * 开通支付服务
	 * @param int $appId
	 * @param int $env
	 * @return boolean
	 */
	public static function openPayService($appId, $env)
	{
		$logger = self::getLogger();
		$appModel = new OaAppModel($env, $logger);
		#支付参数，，是否显示区组，默认值，，默认显示到区
		$showAreaServer = PaymentInterface::PAYMENT_SHOW_AREA;
		$app = $appModel->getApp($appId);
		if (!is_array($app)) {
			$logger->err("app not exists: app-$appId");
			return false;
		}
		$payInterface = new PaymentInterface($env);
		$params = array(
			'spId' => $app['company_id'],
			'appId' => $appId
		);
		if ($app['pay_type'] == OaAppModel::APP_PAY_TYPE_OA_ITEMS) {
			$params['DepositConfigEx'] = array(
				'businessType' => PaymentInterface::BUSINESS_TYPE_COMMON,
				'appStatus' => PaymentInterface::PAYMENT_STATUS_TEST
			);
		} else if ($app['pay_type'] == OaAppModel::APP_PAY_TYPE_OA_TRANSFER) {
			if (empty($app['game_currency_name'])) {
				$logger->err("game currency name is empty: app-$appId");
				return false;
			}
			if (empty($app['game_currency_rate'])) {
				$logger->err("game currency rate is empty: app-$appId");
				return false;
			}
			$appConfig = $appModel->getAppConfig($appId, array('service_pay'), $env);
			if($appConfig && isset($appConfig['service_pay']['showAreaServer'])) {
				$showAreaServer = $appConfig['service_pay']['showAreaServer'];
			}
			$params['TransferConfigEx'] = array(
				'currencyName' => $app['game_currency_name'],
				'currencyUnit' => $app['game_currency_measurement'] . $app['game_currency_name'],
				'isLineRate' => 1,
				'pointList' => $app['game_currency_rate'],
				'businessType' => PaymentInterface::BUSINESS_TYPE_COMMON,
				'showAreaServer' => $showAreaServer,
				'appStatus' => PaymentInterface::PAYMENT_STATUS_TEST
			);
		} else {
			$logger->err("pay type invalid: pay_type-{$app['pay_type']}");
			return false;
		}
		if (!$payInterface->open($params, $env)) {
			$logger->err("open pay service failed: app-$appId, env-$env, errno-" . $payInterface->getErrNo() . " error-" . $payInterface->getErrMsg());
			return false;
		}
		$serviceSync = new OaServiceSync();
		if (!$serviceSync->updatePayPermission($app['company_id'], $appId, $env)) {
			$logger->err("update pay permission failed: app-$appId, env-$env");
			return false;
		}
        sleep(4);
		if (!$serviceSync->syncPaymentArea($app['company_id'], $appId, $env, true)) {
			$logger->err("sync pay area failed: app-$appId, env-$env");
			return false;
		}
		return true;
	}
	
	/**
	 * 更新支付服务
	 * @param int $appId
	 * @param int $env
	 * @return boolean
	 */
	public static function updatePayService($appId, $env)
	{
		$logger = self::getLogger();
		$appModel = new OaAppModel($env, $logger);
		$app = $appModel->getApp($appId);
		#支付参数，，是否显示区组，默认值，，默认显示到区
		$showAreaServer = PaymentInterface::PAYMENT_SHOW_AREA;
		if (!is_array($app)) {
			$logger->err("app not exists: app-$appId");
			return false;
		}
		$payInterface = new PaymentInterface($env);
		$params = array(
			'spId' => $app['company_id'],
			'appId' => $appId
		);
		$appStatus = ($env == OaAppModel::APP_CONFIG_ENV_PRODUCTION && 
			$app['status'] == OaAppModel::APP_STATUS_IN_OPERATION) ? 
			PaymentInterface::PAYMENT_STATUS_ONLINE : PaymentInterface::PAYMENT_STATUS_TEST;	
		if ($app['pay_type'] == OaAppModel::APP_PAY_TYPE_OA_ITEMS) {
			$params['DepositConfigEx'] = array(
				'businessType' => PaymentInterface::BUSINESS_TYPE_COMMON,
				'appStatus' => $appStatus
			);
		} else if ($app['pay_type'] == OaAppModel::APP_PAY_TYPE_OA_TRANSFER) {
			if (empty($app['game_currency_name'])) {
				$logger->err("game currency name is empty: app-$appId");
				return false;
			}
			if (empty($app['game_currency_rate'])) {
				$logger->err("game currency rate is empty: app-$appId");
				return false;
			}
			$appConfig = $appModel->getAppConfig($appId, array('service_pay'), $env);
			if($appConfig && isset($appConfig['service_pay']['showAreaServer'])) {
				$showAreaServer = $appConfig['service_pay']['showAreaServer'];
			}
			$params['TransferConfigEx'] = array(
				'currencyName' => $app['game_currency_name'],
				'currencyUnit' => $app['game_currency_measurement'] . $app['game_currency_name'],
				'isLineRate' => 1,
				'pointList' => $app['game_currency_rate'],
				'businessType' => PaymentInterface::BUSINESS_TYPE_COMMON,
				'showAreaServer' => $showAreaServer,
				'appStatus' => $appStatus
			);
		} else {
			$logger->err("pay type invalid: pay_type-{$app['pay_type']}");
			return false;
		}
		if (!$payInterface->update($params, $env)) {
			$logger->err("update pay service failed: app-$appId, env-$env, errno-" . $payInterface->getErrNo() . " error-" . $payInterface->getErrMsg());
			return false;
		}
		$oasSyncInc = new OaServiceSync();
        $oasSyncInc->updatePayPermission($app['company_id'], $appId, $env);
        sleep(4);
        $oasSyncInc->syncPaymentArea ($app['company_id'], $appId, $env, true);
		return true;
	}
	
	/**
	 * 开通彩虹帮助服务
	 * @param int $appId
	 * @param int $env
	 * @return boolean
	 */
	public static function openHelpService($appId, $env)
	{
		$logger = self::getLogger();
		$appModel = new OaAppModel($env, $logger);
		$app = $appModel->getApp($appId);
		if (!is_array($app)) {
			$logger->err("app not exists: app-$appId");
			return false;
		}
		$helpInterface = new HelpInterface($env);
		$params = array(
			'spId' => $app['company_id'],
			'appId' => $appId
		);
		if (!$helpInterface->open($params, $env)) {
			$logger->err("open help service failed: app-$appId, env-$env, errno-" . $helpInterface->getErrNo() . " error-" . $helpInterface->getErrMsg());
			return false;
		}
		return true;
	}
	
	/**
	 * 更新彩虹帮助服务
	 * @param int $appId
	 * @param int $env
	 * @return boolean
	 */
	public static function updateHelpService($appId, $env)
	{
		$logger = self::getLogger();
		$appModel = new OaAppModel($env, $logger);
		$app = $appModel->getApp($appId);
		if (!is_array($app)) {
			$logger->err("app not exists: app-$appId");
			return false;
		}
		$helpInterface = new HelpInterface($env);
		$params = array(
			'spId' => $app['company_id'],
			'appId' => $appId
		);
		if (!$helpInterface->update($params, $env)) {
			$logger->err("update help service failed: app-$appId, env-$env, errno-" . $helpInterface->getErrNo() . " error-" . $helpInterface->getErrMsg());
			return false;
		}
		return true;
	}
	
	/**
	 * 开通分红服务
	 * @param int $appId
	 * @param int $env
	 * @return boolean
	 */
	public static function openBonusService($appId, $env)
	{
		$logger = self::getLogger();
		$appModel = new OaAppModel($env, $logger);
		$app = $appModel->getApp($appId);
		if (!is_array($app)) {
			$logger->err("app not exists: app-$appId");
			return false;
		}
		$companyModel = new OaCompanyModel($logger);
		$company = $companyModel->getCompany($app['company_id']);
		if (!is_array($company)) {
			$logger->err("company not exists: company-{$app['company_id']}");
			return false;
		}
		$bonusInterface = new DividendInterface($env);
		$params = array(
			'spId' => $app['company_id'],
			'appId' => $appId,
			'sp' => array(
				'spName' => $company['name'],
				'spAddr' => $company['address'],
				'spIntroduce' => $company['intro'],
				'spPhone' => $company['phone'],
				'spMobile' => $company['mobile'],
				'spUser' => $company['legal_person_name'],
				'spEMail' => $company['email']
			)
		);
		if (!$bonusInterface->open($params, $env)) {
			$logger->err("open bonus service failed: app-$appId, env-$env, errno-" . $bonusInterface->getErrNo() . " error-" . $bonusInterface->getErrMsg());
			return false;
		}
		return true;
	}
	
	/**
	 * 更新分红服务
	 * @param int $appId
	 * @param int $env
	 * @return boolean
	 */
	public static function updateBonusService($appId, $env)
	{
		$logger = self::getLogger();
		$appModel = new OaAppModel($env, $logger);
		$app = $appModel->getApp($appId);
		if (!is_array($app)) {
			$logger->err("app not exists: app-$appId");
			return false;
		}
		$companyModel = new OaCompanyModel($logger);
		$company = $companyModel->getCompany($app['company_id']);
		if (!is_array($company)) {
			$logger->err("company not exists: company-{$app['company_id']}");
			return false;
		}
		$bonusInterface = new DividendInterface($env);
		$params = array(
			'spId' => $app['company_id'],
			'appId' => $appId,
			'sp' => array(
				'spName' => $company['name'],
				'spAddr' => $company['address'],
				'spIntroduce' => $company['intro'],
				'spPhone' => $company['phone'],
				'spMobile' => $company['mobile'],
				'spUser' => $company['legal_person_name'],
				'spEMail' => $company['email']
			)
		);
		if (!$bonusInterface->update($params, $env)) {
			$logger->err("update bonus service failed: app-$appId, env-$env, errno-" . $bonusInterface->getErrNo() . " error-" . $bonusInterface->getErrMsg());
			return false;
		}
		return true;
	}
	
	/**
	 * 开通积分服务
	 * @param int $appId
	 * @param int $env
	 * @return boolean
	 */
	public static function openPointsService($appId, $env)
	{
		$logger = self::getLogger();
		$appModel = new OaAppModel($env, $logger);
		$app = $appModel->getApp($appId);
		if (!is_array($app)) {
			$logger->err("app not exists: app-$appId");
			return false;
		}
		$companyModel = new OaCompanyModel($logger);
		$company = $companyModel->getCompany($app['company_id']);
		if (!is_array($company)) {
			$logger->err("company not exists: company-{$app['company_id']}");
			return false;
		}
		$pointsInterface = new IntegralInterface($env);
		$params = array(
			'spId' => $app['company_id'],
			'appId' => $appId,
			'sp' => array(
				'spName' => $company['name'],
				'spAddr' => $company['address'],
				'spProductIntro' => $company['intro'],
				'spTelphone' => $company['phone'],
				'spPhoneNumber' => $company['mobile'],
				'spContactPeron' => $company['legal_person_name'],
				'spEmail' => $company['email']
			)
		);
		if (!$pointsInterface->update($params, $env)) {
			$logger->err("open points service failed: app-$appId, env-$env, errno-" . $pointsInterface->getErrNo() . " error-" . $pointsInterface->getErrMsg());
			return false;
		}
		return true;
	}
	
	/**
	 * 更新积分服务
	 * @param int $appId
	 * @param int $env
	 * @return boolean
	 */
	public static function updatePointsService($appId, $env)
	{
		$logger = self::getLogger();
		$appModel = new OaAppModel($env, $logger);
		$app = $appModel->getApp($appId);
		if (!is_array($app)) {
			$logger->err("app not exists: app-$appId");
			return false;
		}
		$companyModel = new OaCompanyModel($logger);
		$company = $companyModel->getCompany($app['company_id']);
		if (!is_array($company)) {
			$logger->err("company not exists: company-{$app['company_id']}");
			return false;
		}
		$pointsInterface = new IntegralInterface($env);
		$params = array(
			'spId' => $app['company_id'],
			'appId' => $appId,
			'sp' => array(
				'spName' => $company['name'],
				'spAddr' => $company['address'],
				'spProductIntro' => $company['intro'],
				'spTelphone' => $company['phone'],
				'spPhoneNumber' => $company['mobile'],
				'spContactPeron' => $company['legal_person_name'],
				'spEmail' => $company['email']
			)
		);
		if (!$pointsInterface->update($params, $env)) {
			$logger->err("update points service failed: app-$appId, env-$env, errno-" . $pointsInterface->getErrNo() . " error-" . $pointsInterface->getErrMsg());
			return false;
		}
		return true;
	}
}